# -*- coding: utf-8 -*-
"""
Created on Wed Apr 24 10:14:23 2024

@author: 刘翼
"""

import cv2

# Read the input image
img = cv2.imread(r"C:\Users\Public\opencv\Figure\shape.png")

# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply thresholding in the gray image to create a binary image
ret,thresh = cv2.threshold(gray,127,255,0)

# Find the contours using binary image
contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours in image:",len(contours))

# compute the area and perimeter for each contour
for cnt in contours:
    area = cv2.contourArea(cnt)
    perimeter = cv2.arcLength(cnt, True)
    perimeter = round(perimeter, 4)
    print('Area:', area)
    print('Perimeter:', perimeter)
    img1 = cv2.drawContours(img, [cnt], -1, (0,255,0), 3)
    x1, y1 = cnt[0,0]
    cv2.putText(img1, f'Area:{area}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
    cv2.putText(img1, f'Perimeter:{perimeter}', (x1, y1+20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

